//
//  RMLocationManager.m
//  RMNote
//
//  Created by lxf on 16/4/28.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMLocationManager.h"
#import <CoreLocation/CoreLocation.h>

@interface RMLocationManager ()<CLLocationManagerDelegate>
@property (nonatomic, strong) completeGetLocation location;
@property (nonatomic, strong) errorblock error;
@property(nonatomic,strong)CLLocationManager * locationManager;
@property(nonatomic,strong)CLGeocoder * geoCoder;

@end

@implementation RMLocationManager


+ (instancetype)defaultManager{
    static RMLocationManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[RMLocationManager alloc]init];
        
    });
    return manager;
}

- (void)getLocation_complete:(completeGetLocation)complete error:(errorblock)error{
    [self.locationManager startUpdatingLocation];
    self.location = complete;
    self.error = error;
}


#pragma mark ----定位
-(CLLocationManager *)locationManager{
    
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            [_locationManager requestWhenInUseAuthorization];
            [_locationManager requestAlwaysAuthorization];
        }
        // 7.需要在plist文件做设置
        //    NSLocationAlwaysUsageDescription
        //    NSLocationWhenInUseUsageDescription  可添加提示信息
        
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        
    }
    return _locationManager;
    
    
}

-(CLGeocoder *)geoCoder{
    
    if (!_geoCoder) {
        _geoCoder = [[CLGeocoder alloc]init];
    }
    return _geoCoder;
}


#pragma mark - CLLocationManagerDelegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    // 实时获取用户位置
    dispatch_async(dispatch_get_main_queue(), ^{
        
        CLLocation * location = [locations lastObject];
        NSLog(@"经度:%f,维度:%f",location.coordinate.longitude,location.coordinate.latitude);
        
        self.geoCoder = [[CLGeocoder alloc]init];
        [self.geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray * array , NSError *  error) {
            if (array.count > 0) {
                CLPlacemark * placemark = [array objectAtIndex:0];
                NSString * city = placemark.locality;
                if (!city) {
                    city = placemark.administrativeArea;
                }
                self.location(city);
                NSLog(@"city = %@",city);
               
            }else if (error == nil && array.count == 0){
                NSLog(@"success");
            }else if (error){
                NSLog(@"error = %@",error);
            }
            
        }];
    });
    [_locationManager stopUpdatingLocation];
}


-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    
    if (error) {
        self.error(error);
    }
    

    
}



@end
